<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>阻止事件冒泡</title>
    <style>
      #grandFather {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 200px;
        height: 200px;
        border: 2px solid skyblue;
      }

      #parent {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 120px;
        height: 120px;
        border: 2px solid rgb(43, 105, 20);
      }

      #son {
        width: 60px;
        height: 60px;
        border: 2px solid rgb(220, 32, 151);
      }
    </style>
  </head>
  <body>
    <div id="grandFather">
      <div id="parent">
        <div id="son"></div>
      </div>
    </div>
    <script>
      let grandFatherEle = document.getElementById("grandFather");
      let parentEle = document.getElementById("parent");
      let sonEle = document.getElementById("son");

      grandFatherEle.addEventListener("click", function () {
        console.log("grandFather");
      });

      parentEle.addEventListener("click", function () {
        console.log("parent");
      });

      sonEle.addEventListener("click", function (event) {
        // 阻止事件冒泡
        event.stopPropagation();
        console.log("son");
      });
    </script>
  </body>
</html>
